﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Networks.Handlers
{
    public class ConnectorHandler
    {
        public static void onRespLogin(JObject data)
        {
            if (data.ContainsKey("code"))
            {
                int resultCode = Convert.ToInt32(data["code"]);
                if (resultCode == (int)Networks.GAME.LOGIN_CODE.SUCCESS)
                {
                    Debug.Log("Login success.");

                    if (data.ContainsKey("users"))
                    {
                        string[] users = JsonConvert.DeserializeObject<string[]>(data["users"].ToString());
                        Game.Instance.users = users;
                    }

                    SceneManager.LoadScene("Lobby");
                }
                else if (resultCode == (int)Networks.GAME.LOGIN_CODE.DUPLICATE)
                {
                    Debug.Log("Duplicate login.");
                }
                else
                {
                    Debug.Log("Connection error.");
                }
            }
        }
    }
}